<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 18-8-10
 * Time: 下午5:45
 */

namespace common\models\db;


/**
 * Class EquipmentUser
 * @package common\models\db
 *
 * @property EquipmentOperateLog[] $equipmentLoginSuccessLogs
 * @property EquipmentOperateLog $lastEquipmentLoginSuccessLog
 * @property Equipment $equipment
 * @property User $user
 */
class EquipmentUser extends \common\models\db\tables\EquipmentUser
{
    const STATUS_ACTIVE = 10;
    const STATUS_LOGOUT = 9;

    public function generateToken()
    {
        $token = \Yii::$app->security->generateRandomString(50);
        if (self::findOne(['token' => $token])){
            $this->generateToken();
        }else{
            $this->token = $token;
        }
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEquipmentLoginSuccessLogs()
    {
        return $this->hasMany(EquipmentOperateLog::className(), ['success_for_equipment_user' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getLastEquipmentLoginSuccessLog()
    {
        return $this->hasOne(EquipmentOperateLog::className(), ['success_for_equipment_user' => 'id'])->orderBy(['created_at' => SORT_DESC]);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEquipment()
    {
        return $this->hasOne(Equipment::className(), ['id' => 'equipment_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }
}